/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Artist;
import javax.swing.JOptionPane;
import DB.DBmanager;
import POJOS.MusicGroup;
import javax.swing.ListSelectionModel;

/**
 *
 * @author jim
 */
public class ListArtistForm extends javax.swing.JFrame {
    int selectedRow;
    Artist artist;
    MusicGroup musicGroup;
    
    /**
     * Creates new form ListArtistForm
     */
    public ListArtistForm() {
        initComponents();
        checkButtons();
        //Δικαίωμα επιλογής μίας γραμμής στον πίνακα των καλλιτεχνών
        artistTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    //Ελέγχει εάν είναι επιλεγμένη κάποια γραμμή στο πίνακα. Εάν είναι, ενεργοποιεί το κουμπί της τροποποίησης καλλιτέχνη
    private void checkButtons(){
        selectedRow= artistTable.getSelectedRow();
        modifyArtist.setEnabled(selectedRow>=0);
        
        if (selectedRow>=0){
            artist= artistList.get(artistTable.convertRowIndexToModel(selectedRow));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager0 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        artistQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager0.createQuery("SELECT a FROM Artist a");
        artistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(artistQuery.getResultList());
        jLabel1 = new javax.swing.JLabel();
        exit = new javax.swing.JButton();
        newdeleteArtist = new javax.swing.JButton();
        modifyArtist = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        artistTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΑΡΧΕΙΟ ΚΑΛΛΙΤΕΧΝΩΝ");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Πίνακας Καλλιτεχνών");
        jLabel1.setToolTipText("");

        exit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-exit-icon.jpg"))); // NOI18N
        exit.setText("ΕΞΟΔΟΣ");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        newdeleteArtist.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        newdeleteArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add-remove-user-icons-20638746.jpg"))); // NOI18N
        newdeleteArtist.setText("ΠΡΟΣΘΗΚΗ-ΔΙΑΓΡΑΦΗ");
        newdeleteArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newdeleteArtistActionPerformed(evt);
            }
        });

        modifyArtist.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        modifyArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/hp-customization-control-panel-icon.png"))); // NOI18N
        modifyArtist.setText("ΤΡΟΠΟΠΟΙΗΣΗ");
        modifyArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyArtistActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artistList, artistTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastname}"));
        columnBinding.setColumnName("ΕΠΩΝΥΜΟ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstname}"));
        columnBinding.setColumnName("ΟΝΟΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artisticName}"));
        columnBinding.setColumnName("ΚΑΛΛΙΤΕΧΝΙΚΟ ΟΝΟΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sex}"));
        columnBinding.setColumnName("ΦΥΛΟ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${birthday}"));
        columnBinding.setColumnName("ΗΜΕΡΟΜΗΝΙΑ ΓΕΝΝΗΣΗΣ");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${birthPlace}"));
        columnBinding.setColumnName("ΤΟΠΟΣ ΓΕΝΝΗΣΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idMusicGenre.nameMusicGenre}"));
        columnBinding.setColumnName("ΕΙΔΟΣ ΜΟΥΣΙΚΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        artistTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                artistTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(artistTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(newdeleteArtist)
                .addGap(83, 83, 83)
                .addComponent(modifyArtist)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 713, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newdeleteArtist)
                        .addGap(47, 47, 47)
                        .addComponent(modifyArtist)
                        .addGap(49, 49, 49)
                        .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newdeleteArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modifyArtist)
                    .addComponent(exit))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
        //Επιβεβαίωση για την έξοδο
        int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε έξοδο από τη διαχείριση καλλιτεχνών;","",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); 
        if (choice==0){
            dispose();
        }else
            checkButtons();
    }//GEN-LAST:event_exitActionPerformed

    private void newdeleteArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newdeleteArtistActionPerformed
        // TODO add your handling code here:
        //Εάν δεν είναι επιλεγμένη κάποια γραμμή μεταβαίνει στο GUI "προσθήκη καλλιτέχνη"
        if (selectedRow<0 ){
            new NewOrModifyArtistForm().setVisible(true);
        //Εάν είναι επιλεγμένη κάποια γραμμή διαγράφει τον επιλεγμένο καλλιτέχνη στον πίνακα
        }else{
            selectedRow= artistTable.getSelectedRow();
            artist= artistList.get(artistTable.convertRowIndexToModel(selectedRow));
            
            //Επιβεβαίωση για τη διαγραφή
            int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε να διαγράψετε τον καλλιτένη " + artist.getArtisticName() + "" +
                " ;","",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        
            if (choice==0){
               //Έλεγχος αν ο καλλιτέχνης ανήκει σε συγκρότημα
                if(artist.getMusicGroupCollection().isEmpty()){
                    //Έλεγχος αν ο καλλιτέχνης έχει άλμπουμ
                    if(artist.getAlbumCollection().isEmpty()){
              
                        //Διαγραφή καλλιτέχνη από τη βάση δεδομένων
                        if (DB.DBmanager.DeleteArtist(artist)){
                        //Διαγραφή καλλιτέχνη από τη λίστα
                            artistList.remove(artist);
                        JOptionPane.showMessageDialog(null,"Επιτυχής διαγραφή καλλιτέχνη!","SUCCESSFULL",JOptionPane.INFORMATION_MESSAGE);
                        }else{
                            JOptionPane.showMessageDialog(null,"Αποτυχία διαγραφής καλλιτέχνη!","ERROR",JOptionPane.ERROR_MESSAGE);
                        }
                        //Αν ο καλλιτέχνης έχει άλμπουμ αδύνατη διαγραφή
                    }else{
                        JOptionPane.showMessageDialog(null,"Αδύνατη Διαγραφή!Ο καλλιτέχνης περιέχει άλμπουμ!","ERROR",JOptionPane.ERROR_MESSAGE);
                    }
                    //Αν ο καλλιτέχνης ανήκει σε συγκρότημα αδύνατη διαγραφή
                }else{
                    JOptionPane.showMessageDialog(null,"Αδύνατη Διαγραφή!Ο καλλιτέχνης είναι μέλος συγκροτήματος!","ERROR",JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        checkButtons();
    }//GEN-LAST:event_newdeleteArtistActionPerformed
    

    private void modifyArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyArtistActionPerformed
        // TODO add your handling code here:
        //Θέτει στο αντικείμενο artist τα στοιχεία του καλλιτέχνη από τον πίνακα και μεταβαίνει στο GUI για την τροποποίηση 
        //των στοιχείων του
        selectedRow= artistTable.getSelectedRow();
        artist= artistList.get(artistTable.convertRowIndexToModel(selectedRow));
        new NewOrModifyArtistForm(artist).setVisible(true);
    }//GEN-LAST:event_modifyArtistActionPerformed

    private void artistTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_artistTableMouseClicked
        // TODO add your handling code here:
        checkButtons();
    }//GEN-LAST:event_artistTableMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        //Εάν κάνουμε κλικ κάπου στη φόρμα εκτός πίνακα, ενεργοποιεί τη προσθήκη καλλιτέχνη
        //και απενοργοποιεί την τροποποίηση και τη διαγραφή
        selectedRow=-1;
        modifyArtist.setEnabled(selectedRow>=0);
    }//GEN-LAST:event_formMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListArtistForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager0;
    public static java.util.List<POJOS.Artist> artistList;
    private javax.persistence.Query artistQuery;
    public static javax.swing.JTable artistTable;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton modifyArtist;
    private javax.swing.JButton newdeleteArtist;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
